<?php

App::uses('Controller', 'Controller');

/**
 * Class AppController
 */
class AppController extends Controller {

	public $layout = 'home';

	public $ext = '.html';

	/**
	 * Component
	 *
	 * @type array
	 */
	public $components = [
		'DebugKit.Toolbar',
		'Session',
		'Acl',
		'Auth' => [
//			'authenticate' => array('Basic' => array(
//				'fields' => array(
//					'username' => 'username',
//					'password' => 'password'
//				),
//				'userModel' => 'User',
//				'scope' => array(
//					array('User.is_active' => 1)
//				),
//				'recursive' => -1,
//				'contain' => null,
//				'realm' => 'Failure'
//			)),
			'authorize' => [
				'Actions' => ['actionPath' => 'controllers']
			],
		],
	];

	/**
	 * Hepler
	 *
	 * @type array
	 */
	public $helpers = ['Html', 'Form', 'Session'];

	/**
	 * Initialization
	 *
	 * @return void
	 */
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->loginAction = ['controller' => 'users', 'action' => 'login',];
		$this->Auth->loginRedirect = ['controller' => 'users', 'action' => 'login',];
		$this->Auth->logoutRedirect = ['controller' => 'users', 'action' => 'login',];
//		AuthComponent::$sessionKey = false;
	}

	/**
	 * Check if Ajax
	 *
	 * @return void
	 */
	public function isAjax() {
		if (!$this->request->is('ajax')) {
			throw new MethodNotAllowedException();
		}
		$this->autoLayout = $this->autoRender = false;
	}

	/**
	 * Load frame
	 *
	 * @return void
	 */
	public function loadFrame() {
		$this->layout = 'frame';
		$this->ext = '.html';
	}

	/**
	 * Success information
	 *
	 * @param string $info
	 * @param string $url
	 * @return array
	 */
	public function success($info, $url = '') {
		die(json_encode(['status' => true, 'info' => $info, 'url' => $url]));
	}

	/**
	 * Error information
	 *
	 * @param string $info
	 * @param string $url
	 * @return void
	 */
	public function error($info, $url = '') {
		die(json_encode(['status' => false, 'info' => $info, 'url' => $url]));
	}
}
